package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.base.PageResult;
import com.crm.base.ResponseResult;
import com.crm.domain.dto.BusinessDto;
import com.crm.domain.entity.Business;
import com.crm.domain.vo.BusinessVo;
import com.crm.mapper.PublicPoolMapper;
import com.crm.service.PublicPoolService;
import com.crm.utils.HttpStatus;
import com.crm.utils.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Consts;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class PublicPoolServiceImpl extends ServiceImpl<PublicPoolMapper, Business> implements PublicPoolService {

    private final PublicPoolMapper publicPoolMapper;

    /**
     * 分页查询公海池列表
     *
     * @param businessDto
     * @return
     */
    @Override
    public PageResult<BusinessVo> queryAll(BusinessDto businessDto) {
        // 分页查询公海池列表
        Page<Business> page = Page.of(businessDto.getPageNum(), businessDto.getPageSize());
        // 构建查询条件
        LambdaQueryWrapper<Business> queryWrapper = Wrappers.<Business>lambdaQuery()
                .eq(businessDto.getId() != null, Business::getId, businessDto.getId())
                .eq(businessDto.getSubject() != null, Business::getSubject, businessDto.getSubject())
                .like(businessDto.getName() != null, Business::getName, businessDto.getName())
                .like(businessDto.getPhone() != null, Business::getPhone, businessDto.getPhone())
                .ge(ObjectUtil.isNotEmpty(businessDto.getBeginCreateTime()), Business::getBeginCreateTime, businessDto.getBeginCreateTime())
                .le(ObjectUtil.isNotEmpty(businessDto.getEndCreateTime()), Business::getEndCreateTime, businessDto.getEndCreateTime());

        // 执行分页查询
        Page<Business> businessPage = publicPoolMapper.selectPage(page, queryWrapper);

        // 结果转换
        List<BusinessVo> businessVoList = businessPage.getRecords().stream()
                .map(business -> {
                    BusinessVo businessVo = new BusinessVo();
                    BeanUtils.copyProperties(business, businessVo);
                    return businessVo;
                })
                .collect(Collectors.toList());

        // 构建分页响应
        PageResult<BusinessVo> pageResult = new PageResult<>();
        pageResult.setTotal(businessVoList.size());
        pageResult.setRows(businessVoList);
        pageResult.setMsg("查询成功");
        pageResult.setCode(HttpStatus.SUCCESS);

        return pageResult;
    }

    /**
     * 踢回公海池
     *
     * @param id
     * @param reasonId
     * @return
     */
    @Override
    public ResponseResult backPool(Long id, Long reasonId) {
        //根据id查找要提出公海池的用户
        Business business = publicPoolMapper.selectById(id);
        if (business == null) {
            return ResponseResult.error(HttpStatus.NOT_FOUND, "该用户不存在");
        }
        //将该用户的状态设置
        business.setIsExist(0);
        //设置提出公海池的原因
        business.setReasons(String.valueOf(reasonId));
        //踢出公海池
        publicPoolMapper.updateById(business);
        return ResponseResult.success("踢出出公海池成功");
    }
}

